package org.devbg.academy.corejava.shop.ui.models;

import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.devbg.academy.corejava.shop.db.daos.ProductDAO;
import org.devbg.academy.corejava.shop.db.entities.Category;
import org.devbg.academy.corejava.shop.db.entities.Product;

public class ProductTableModel extends AbstractTableModel {
	/**
	 * The serial version UID of the class.
	 */
	private static final long serialVersionUID = 1L;

	private ProductDAO dao = new ProductDAO();
	private List<Product> products = Collections.<Product> emptyList();

	public void changeCategory(Category category) {
		this.products = dao.findByCategory(category);
		fireTableDataChanged();
	}

	private static final String[] COLUMN_NAMES = new String[] { "ID", "Име",
			"Цена" };

	@Override
	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}

	public ProductTableModel() {
	}

	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	public int getRowCount() {
		return products.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return getValue(products.get(rowIndex), columnIndex);
	}

	private Object getValue(Product product, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return product.getId();
		case 1:
			return product.getName();
		case 2:
			return product.getPrice();
		default:
			return null;
		}
	}

}
